package com.atguigu.lease.admin.controller;

import com.atguigu.lease.admin.service.ApartmentInfoService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.ApartmentInfo;
import com.atguigu.lease.model.vo.ApartmentDetailVo;
import com.atguigu.lease.model.vo.ApartmentItemVo;
import com.atguigu.lease.model.vo.ApartmentQueryVo;
import com.atguigu.lease.model.vo.ApartmentSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LOGIN
 * @title ApartmentController
 * @create 2025/6/24 21:07
 * @description
 */
@Tag(name="公寓管理模块")
@RestController
@RequestMapping("/admin/apartment")
public class ApartmentController {
    @Autowired
    private ApartmentInfoService apartmentInfoService;

//    /admin/apartment/listInfoByDistrictId`
    @Operation(summary = "6、根据区县id查询公寓列表")
    @GetMapping("/listInfoByDistrictId")
    public Result<List<ApartmentInfo>> listInfoByDistrictId(@RequestParam Long id){
        //条件查询构造器
        LambdaQueryWrapper<ApartmentInfo> queryWrapper = new LambdaQueryWrapper<>(ApartmentInfo.class);
        // select * from ApartmentInfo where district_id = #{Id}
        queryWrapper.eq(ApartmentInfo::getDistrictId, id);
        List<ApartmentInfo> apartmentInfos = apartmentInfoService.list(queryWrapper);
        return Result.ok(apartmentInfos);
    }

//    /updateReleaseStatusById
    @Operation(summary = "5、修改公寓发布状态")
    @PostMapping("/updateReleaseStatusById")
    public Result updateReleaseStatusById(
            @RequestParam Long id,
            @RequestParam String status ){
        apartmentInfoService.update(Wrappers.lambdaUpdate(ApartmentInfo.class)
                .eq(ApartmentInfo::getId,id)
                .set(ApartmentInfo::getIsRelease,status));
        return Result.ok();
    }

//   /admin/apartment/removeById
    @Operation(summary = "4、根据id删除公寓信息")
    @DeleteMapping("/removeById")
    public Result removeById(@RequestParam Long id){
        apartmentInfoService.deleteApartmentById(id);
        return Result.ok();
    }

//    /admin/apartment/getDetailById
    @Operation(summary = "3、根据ID获取公寓详细信息")
    @GetMapping("/getDetailById")
    public Result getDetailById(@RequestParam Long id){
        ApartmentDetailVo apartmentDetailVo= apartmentInfoService.getDetailById(id);
        return Result.ok(apartmentDetailVo);

    }

//    /admin/apartment/pageItem?current=1&size=10
    @Operation(summary = "2、根据条件分页查询公寓列表")
    @GetMapping("/pageItem")
    public Result<Page<ApartmentItemVo>> pageItem(
                        @RequestParam Long current,
                        @RequestParam Long size,
                        //pojo入参：接受多个请求参数，独享不能加@RequestParam注解
                        ApartmentQueryVo queryVo
            ){
        Page<ApartmentItemVo> page=  apartmentInfoService.getPageItem(current,size,queryVo);
        //响应数据分析： Page<ApartmentItemVo>
        //响应数据 需要查询公寓的数据 和公寓的房间信息，所以涉及多表联查 需要自定义sql
        return Result.ok(page);
    }

    @Operation(summary = "1、新增或更新公寓")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody ApartmentSubmitVo apartmentSubmitVo){
        apartmentInfoService.saveOrUpdateApartmentSubmitVo(apartmentSubmitVo);
        return Result.ok();
    }
}
